﻿<template>
  <a-row :gutter="10">
    <a-col span="12">
      <a-card title="分类信息 > 点击分类右侧展示详情" colspan="12" :bordered="false">
        <div class="table-operator">
          <a-button v-if="hasPerm('Base_BrandGroup.Add')" type="primary" icon="plus" @click="handleAdd()">新建</a-button>
          <a-button
            v-if="hasPerm('Base_BrandGroup.Delete')"
            type="primary"
            icon="minus"
            @click="handleDelete(selectedRowKeys)"
            :disabled="!hasSelected()"
            :loading="loading"
          >删除
          </a-button>
          <a-button type="primary" icon="redo" @click="getDataList()">刷新</a-button>
        </div>
        <!--        <div class="table-page-search-wrapper">-->
        <!--          <a-form layout="inline">-->
        <!--            <a-row :gutter="10">-->
        <!--              <a-col :md="4" :sm="24">-->
        <!--                <a-form-item label="查询类别">-->
        <!--                  <a-select allowClear v-model="queryParam.condition">-->
        <!--                    &lt;!&ndash;<a-select-option key="Code">自定义编码</a-select-option>&ndash;&gt;-->
        <!--                    <a-select-option key="Value">分组名称</a-select-option>-->
        <!--                    <a-select-option key="Remark">备注</a-select-option>-->
        <!--                  </a-select>-->
        <!--                </a-form-item>-->
        <!--              </a-col>-->
        <!--              <a-col :md="4" :sm="24">-->
        <!--                <a-form-item>-->
        <!--                  <a-input v-model="queryParam.keyword" placeholder="关键字" />-->
        <!--                </a-form-item>-->
        <!--              </a-col>-->
        <!--              <a-col :md="6" :sm="24">-->
        <!--                <a-button type="primary" @click="() => {this.pagination.current = 1; this.getDataList()}">查询</a-button>-->
        <!--                <a-button style="margin-left: 8px" @click="() => (queryParam = {})">重置</a-button>-->
        <!--              </a-col>-->
        <!--            </a-row>-->
        <!--          </a-form>-->
        <!--        </div>-->

        <a-table
          ref="table"
          :scroll="{y:530}"
          :columns="columns"
          :rowKey="row => row.Id"
          :dataSource="data"
          :pagination="pagination"
          :loading="loading"
          :customRow="handleRowClick"
          @change="handleTableChange"
          :rowSelection="{ selectedRowKeys: selectedRowKeys, onChange: onSelectChange }"
          :bordered="true"
          size="small">
          <span slot="action" slot-scope="text, record">
            <template>
              <a v-if="hasPerm('Base_BrandGroup.Edit')" @click="handleEdit(record.Id)">编辑</a>
              <a-divider v-if="hasPerm('Base_BrandGroup.Edit')&&hasPerm('Base_BrandGroup.Delete')" type="vertical" />
              <a v-if="hasPerm('Base_BrandGroup.Delete')" @click="handleDelete([record.Id])">删除</a>
            </template>
          </span>
        </a-table>

        <edit-form ref="editForm" :parentObj="this"></edit-form>
      </a-card>
    </a-col>
    <a-col span="12">
      <g-c-list ref="gcList" host-table-name="Base_BrandGroup" slave-table-name="Base_Brand" host-title="品牌分类" slave-title="品牌名称"></g-c-list>
    </a-col>
  </a-row>
</template>

<script>
import EditForm from './EditForm'
import GCList from '@/views/Base_BasicData/Base_GeneralConfig/List'

const columns = [
  { title: '排序', dataIndex: 'Sort' },
  // { title: '自定义编码', dataIndex: 'Code', width: '10%' },
  { title: '分类名称', dataIndex: 'Value' },
  { title: '备注', dataIndex: 'Remark' },
  { title: '操作', dataIndex: 'action', scopedSlots: { customRender: 'action' } }
]

export default {
  components: {
    EditForm,
    GCList
  },
  mounted () {
    this.getDataList()
  },
  data () {
    return {
      data: [],
      pagination: {
        current: 1,
        pageSize: 20,
        showTotal: (total, range) => `总数:${total} 当前:${range[0]}-${range[1]}`,
        pageSizeOptions: ['10', '20', '30', '40', '50', '100'], // 设置pageSize的可选值，页面啊可以通过下拉框进行选择
        showSizeChanger: true
      },
      filters: {},
      sorter: { field: 'Id', order: 'desc' },
      loading: false,
      columns,
      queryParam: {},
      selectedRowKeys: []
    }
  },
  methods: {
    handleTableChange (pagination, filters, sorter) {
      this.pagination = { ...pagination }
      this.filters = { ...filters }
      this.sorter = { ...sorter }
      this.getDataList()
    },
    getDataList () {
      this.selectedRowKeys = []
      this.loading = true
      this.$http
        .post('/Base_BasicData/Base_BrandGroup/GetDataList', {
          PageIndex: this.pagination.current,
          PageRows: this.pagination.pageSize,
          SortField: this.sorter.field || 'Id',
          SortType: this.sorter.order || 'desc',
          Search: this.queryParam,
          ...this.filters
        })
        .then(resJson => {
          this.loading = false
          this.data = resJson.Data
          if (this.data) {
            const pagination = { ...this.pagination }
            pagination.total = resJson.Total
            this.pagination = pagination
          }
        })
      this.InitialInfo()
    },
    onSelectChange (selectedRowKeys) {
      this.selectedRowKeys = selectedRowKeys
    },
    hasSelected () {
      return this.selectedRowKeys.length > 0
    },
    handleAdd () {
      this.$refs.editForm.openForm(null, '新建品牌分类')
    },
    handleEdit (id) {
      this.$refs.editForm.openForm(id, '编辑品牌分类')
    },
    handleDelete (ids) {
      const thisObj = this
      this.$confirm({
        title: '确认删除吗?',
        onOk () {
          return new Promise((resolve, reject) => {
            thisObj.$http.post('/Base_BasicData/Base_BrandGroup/DeleteData', ids).then(resJson => {
              resolve()

              if (resJson.Success) {
                thisObj.$message.success('操作成功!')

                thisObj.getDataList()
              } else {
                thisObj.$message.error(resJson.Msg)
              }
            })
          })
        }
      })
    },
    handleRowClick (record, index) {
      return {
        on: {
          click: () => {
            console.log(record, index)
            this.$refs.gcList.showTitle = record.Value + '详情'
            this.$refs.gcList.hostId = record.Id
            this.$refs.gcList.getDataListByHostId()
          }
        }
      }
    },
    InitialInfo () {
      this.$refs.gcList.columns = [
        { title: '排序', dataIndex: 'Sort' },
        // { title: '主表名称', dataIndex: 'HostTableName' },
        // { title: '主表ID', dataIndex: 'HostId' },
        { title: '分类名称', dataIndex: 'Value' },
        // { title: '从表名称', dataIndex: 'SlaveTableName' },
        { title: '品牌编号', dataIndex: 'SlaveId' },
        { title: '品牌名称', dataIndex: 'Name' },
        // { title: '备注', dataIndex: 'Remark' },
        { title: '操作', dataIndex: 'action', scopedSlots: { customRender: 'action' } }
      ]
      this.$refs.gcList.isAppoint = true
      this.$refs.gcList.showTitle = '品牌分类详情'
      this.$refs.gcList.editFormTitle = '品牌'
      this.$refs.gcList.hostId = ''
      this.$refs.gcList.getDataListByHostId()
    }
  }
}
</script>
